<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>我发布的商品</title>
    <link
      href="https://cdn.bootcdn.net/ajax/libs/twitter-bootstrap/3.4.1/css/bootstrap.min.css"
      rel="stylesheet"
    />
    <!-- <link
      href="https://cdn.bootcdn.net/ajax/libs/twitter-bootstrap/5.0.2/css/bootstrap.min.css"
      rel="stylesheet"
    /> -->
    <script src="https://cdn.bootcdn.net/ajax/libs/jquery/3.6.0/jquery.min.js"></script>
    <script src="Pagination.js"></script>
    <style>
      img {
        width: 80px;
      }
    </style>
  </head>
  <body>
    <div class="container">
      <a class="btn btn-primary" href="/addshop">添加商品</a>
      <a href="/index" class="btn btn-success">返回首页</a>
      <table class="table table-bordered table-hover">
        <thead>
          <tr>
            <th>图片</th>
            <th>商品名</th>
            <th>价格</th>
            <th>描述</th>
            <th>操作</th>
          </tr>
        </thead>
        <tbody id="tb">
          <!-- <tr>
            <td>1</td>
            <td>1</td>
            <td>1</td>
            <td>1</td>
            <td>
              <a href="/edit" class="btn btn-primary">修改商品信息</a>
              <button class="btn btn-danger">删除此商品</button>
            </td>
          </tr> -->
        </tbody>
      </table>
    </div>
    <script>
      // 获取自己发布的商品信息
      refreshMyShop();
      function refreshMyShop() {
        //拿到本地缓存中的uid
        let uid = localStorage.getItem("uid");
        console.log(uid);
        $.ajax({
          url: "/myshoplist",
          type: "get",
          data: {
            uid: uid,
          },
          success: function (res) {
            console.log(res);
            //将获取到的数据渲染到页面上
            let str = "";
            res.data.forEach((item) => {
              str += `<tr>
                <td><img src="${item.pimg}"></td>
                <td>${item.pname}</td>
                <td>${item.pprice}</td>
                <td>${item.pdesc}</td>
                <td>
                 <a href="/editshop?pid=${item._id}" class="btn btn-primary">修改商品信息</a>
                 <button class="btn btn-danger" onclick="delShop('${item._id}')">删除此商品</button>
                </td>
                </tr>`;
            });
            $("#tb").html(str);
          },
        });
      }
      //点击删除
      function delShop(pid) {
        console.log(pid);
        $.ajax({
          url: "/delShop/" + pid,
          type: "delete",
          success: function (res) {
            console.log(res);
            alert(res.msg);
            refreshMyShop();
          },
        });
      }
    </script>
  </body>
</html>
